<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

    <script>
        // var myAtoi = function (s) {
        //     let num = '' //存放可用数字
        //     let flag = true //判断正负---初始为正
        //     let flag2 = false //判断是不是已经存了数
        //     let num1 = Math.pow(-2, 31)
        //     let num2 = Math.pow(2, 31) - 1
        //     for (n in s) {
        //         if (s[n] === '-' && parseInt(s[++n]) && !flag2 ) {
        //             flag = false
        //             continue
        //         }
        //         if (parseInt(s[n])) {
        //             num += s[n]
        //             flag2 = true

        //         } else if (parseInt(num)) {
        //             break
        //         }
        //     }
        //     num = flag ? parseInt(num) : parseInt(num)*-1
        //     if(num > num2){
        //         return num2
        //     }else if( num < num1){
        //         return 0
        //     }
        //     console.log(num,num1);
        //     return num
        // }
        // console.log(myAtoi("words and 987"),'123');

        var myAtoi = function (s) {
            let pre = '', num = '', index = 0;
            let num1 = Math.pow(-2, 31)
            let num2 = Math.pow(2, 31) - 1
            while (s[index] === ' ') {
                index++
            }
            console.log(index);
            while (s[index] === '-' || s[index] === '+') {
                if (pre) {
                    return 0
                }
                pre = s[index]
                index++
            }
            console.log(num, '111');
            while (Number(s[index]) || s[index] == '0') {
                num += s[index]
                index++
            }
            console.log(num, '111');
            num = Number(pre + num) || 0
            num = Math.min(num2, num)
            num = Math.max(num1, num)
            return num
        };
        console.log(myAtoi("   -88827   5655  U"));
    </script>
</body>

</html>